package org.egedede.osgi.guishell;

import java.awt.Component;
import java.util.TreeSet;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.osgi.framework.Bundle;

public final class BundleTreeCellRenderer extends DefaultTreeCellRenderer {
	@Override
	public Component getTreeCellRendererComponent(JTree tree,
			Object value, boolean selected, boolean expanded,
			boolean leaf, int row, boolean hasFocus) {
		super.getTreeCellRendererComponent(tree, value, selected, expanded,
				leaf, row, hasFocus);
		if (value instanceof TreeSet<?>) {
			this.setText("bundles");
			this.setIcon(new ImageIcon(getClass().getClassLoader().getResource("toolbarButtonGraphics/navigation/Home16.gif")));
		} else {
			Bundle bundle = (Bundle) value;
			this.setText(bundle.getBundleId() + " - "
					+ bundle.getSymbolicName());
			String path = "toolbarButtonGraphics/navigation/Home16.gif";
			switch (bundle.getState()) {
			case Bundle.ACTIVE: {
				path = "toolbarButtonGraphics/media/Play16.gif";
				break;
			}
			case Bundle.INSTALLED: {
				path = "toolbarButtonGraphics/development/Jar16.gif";
				break;
			}
			case Bundle.RESOLVED: {
				path = "toolbarButtonGraphics/development/Jar16.gif";
				break;
			}
			case Bundle.STARTING: {
				path = "toolbarButtonGraphics/media/FastForward16.gif";
				break;
			}
			case Bundle.STOPPING: {
				path = "toolbarButtonGraphics/media/Pause16.gif";
				break;
			}
			case Bundle.UNINSTALLED: {
				path = "toolbarButtonGraphics/general/Remove16.gif";
				break;
			}
			default: {
				path = "toolbarButtonGraphics/general/TipOfTheDay16.gif";
				break;
			}
			}
//			System.out.println(path);
			this.setIcon(new ImageIcon(getClass().getClassLoader().getResource(path)));
		}
		return this;
	}

}
